package com.zoffy.wmsbackend.model.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoffy.wmsbackend.model.entity.*;
import com.zoffy.wmsbackend.model.excel.bean.StockInModel;
import com.zoffy.wmsbackend.service.*;
import com.zoffy.wmsbackend.utils.StockUtil;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StockInImportListener extends AnalysisEventListener<StockInModel> {
    @Resource
    private StockInOrderService stockInOrderService;
    @Resource
    private StockInOrderDetailService stockInOrderDetailService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private StockUtil stockUtil;
    private Map<String,Long> warehouseMap;
    private Map<String,Long> goodsMap;
    private Map<String,StockInOrder> stockInOrderMap;
    private List<StockInOrderDetail> stockInOrderDetails;
    public StockInImportListener (){
        this.warehouseMap = warehouseService.list(new QueryWrapper<Warehouse>()).stream().collect(Collectors.toMap(Warehouse::getWarehouseCode,Warehouse::getId));
        this.goodsMap = goodsService.list(new QueryWrapper<Goods>()).stream().collect(Collectors.toMap(Goods::getGoodsCode,Goods::getId));
    }
    @Override
    public void invoke(StockInModel stockInModel, AnalysisContext analysisContext) {
        String warehouseCode = stockInModel.getWarehouseCode();
        String goodsCode = stockInModel.getGoodsCode();
        String stockInNo = stockInModel.getStockInNo();
        Integer quantity = stockInModel.getQuantity();
        if(!warehouseMap.containsKey(warehouseCode)|| !goodsMap.containsKey(goodsCode)){
            return;
        }
        if(!stockInOrderMap.containsKey(stockInNo)){
            StockInOrder stockInOrder = new StockInOrder();
            stockInOrder.setStockInNo(stockInNo);
            stockInOrder.setWarehouseId(warehouseMap.get(warehouseCode));
            stockInOrder.setStatus(0);
            stockInOrderMap.put(stockInNo,stockInOrder);
        }
        StockInOrderDetail stockInOrderDetail = new StockInOrderDetail();
        stockInOrderDetail.setStockInOrderId(stockInOrderMap.get(stockInNo).getId());
        stockInOrderDetail.setWarehouseId(warehouseMap.get(warehouseCode));
        stockInOrderDetail.setGoodsId(goodsMap.get(goodsCode));
        stockInOrderDetail.setQuantity(quantity);
        stockInOrderDetail.setStatus(0);
        stockInOrderDetails.add(stockInOrderDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        stockInOrderDetailService.saveBatch(stockInOrderDetails);
        stockInOrderService.saveBatch(stockInOrderMap.values());
    }
}
